﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace QuaManSys.Rights
{
    public partial class DeptGrant : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)//第一次加载页面
            {
                if (Request.QueryString["department"] != null)
                {
                    lblId.Text = Request.QueryString["department"].ToString();
                    BindRoles();
                }
            }
        }

        private void BindRoles()
        {
            lbOwnedRoles.Items.Clear();
            lbRoles.Items.Clear();
            using (var entity = new QuaManSysEntities())
            {
                var queryAllRoles = from q in entity.SysRole
                                    orderby q.Id
                                    select q;
                var queryOwnedRoles = from q in entity.SysRoleSysDept
                                      join p in entity.SysRole
                                      on q.SysRoleId equals p.Id
                                      where q.Dept == lblId.Text
                                      select p;

                bool ownedOrNot = false;
                foreach (var item in queryAllRoles)
                {
                    foreach (var owned in queryOwnedRoles)
                    {
                        if (owned == item) ownedOrNot = true;
                    }
                    if (ownedOrNot)
                    {
                        lbOwnedRoles.Items.Add(new ListItem(item.Name, item.Id));
                    }
                    else
                    {
                        lbRoles.Items.Add(new ListItem(item.Name, item.Id));
                    }
                    ownedOrNot = false;
                }
            }
        }

        protected void btn_saverole_Click(object sender, EventArgs e)
        {
            using (var entity = new QuaManSysEntities())
            {
                //查询更新前的角色
                var rolesBefore = from q in entity.SysRoleSysDept
                                  where q.Dept == lblId.Text
                                  select q;
                //rolesAfter存放更新后的所有角色Id
                List<string> rolesAfter = new List<string>();
                for (int i = 0; i < this.lbOwnedRoles.Items.Count; i++)
                {
                    rolesAfter.Add(this.lbOwnedRoles.Items[i].Value);
                }
                //先删除原有的所有角色
                foreach (var item in rolesBefore)
                {
                    entity.SysRoleSysDept.Remove(item);
                    //entity.SaveChanges();
                }
                entity.SaveChanges();
                //添加更新后的所有角色
                foreach (var item in rolesAfter)
                {
                    SysRoleSysDept aRole = new SysRoleSysDept()
                    {
                        Dept = lblId.Text,
                        SysRoleId = item,
                    };
                    entity.SysRoleSysDept.Add(aRole);
                    entity.SaveChanges();
                }
                strinfo.Visible = true;
                RolePanel.Visible = false;
            }
        }

        protected void btn_plus_Click(object sender, EventArgs e)
        {
            int c = lbRoles.Items.Count;
            ArrayList altRoles = new ArrayList();
            ArrayList altOwnedRoles = new ArrayList();
            foreach (ListItem item in lbOwnedRoles.Items)
            {
                altOwnedRoles.Add(item);
            }

            foreach (ListItem item in lbRoles.Items)
            {
                if (item.Selected)
                {
                    altOwnedRoles.Add(item);
                }
                else
                {
                    altRoles.Add(item);
                }

            }
            lbRoles.Items.Clear();
            lbOwnedRoles.Items.Clear();
            foreach (ListItem item in altRoles) lbRoles.Items.Add(item);
            foreach (ListItem item in altOwnedRoles) lbOwnedRoles.Items.Add(item);
        }

        protected void btn_subtract_Click(object sender, EventArgs e)
        {
            int c = lbOwnedRoles.Items.Count;
            ArrayList altOwnedRoles = new ArrayList();
            ArrayList altRoles = new ArrayList();
            foreach (ListItem item in lbRoles.Items)
            {
                altRoles.Add(item);
            }

            foreach (ListItem item in lbOwnedRoles.Items)
            {
                if (item.Selected)
                {
                    altRoles.Add(item);
                }
                else
                {
                    altOwnedRoles.Add(item);
                }

            }
            lbOwnedRoles.Items.Clear();
            lbRoles.Items.Clear();
            foreach (ListItem item in altOwnedRoles) lbOwnedRoles.Items.Add(item);
            foreach (ListItem item in altRoles) lbRoles.Items.Add(item);
        }
    }
}